package org.colafries.admin.entity.domain;

import org.colafries.common.enums.ColumnDataTypeEnum;
import org.colafries.common.enums.ColumnTypeEnum;
import org.colafries.common.enums.Flag;
import io.swagger.annotations.ApiModelProperty;
import lombok.Builder;
import lombok.Data;

import java.util.List;
import java.util.Map;

/**
 * @author colafries
 * @version v1.0
 * @ClassName QueryParam
 * @Description SQL查询参数
 * @date Created in 2024/4/12
 **/
@Data
@Builder
public class QueryParam {
    /**
     * 维度
     */
    @ApiModelProperty(value = "维度")
    private List<Dimension> dimensions;
    /**
     * 度量
     */
    @ApiModelProperty(value = "度量")
    private List<Measure> measures;
    /**
     * 筛选条件
     */
    @ApiModelProperty(value = "筛选条件")
    private List<Condition> conditions;
    /**
     * 查询结果列
     */
    @ApiModelProperty(value = "查询结果列")
    private List<ResultColumn> resultColumns;
    /**
     * 过滤条件
     */
    @ApiModelProperty(value = "过滤条件")
    private List<Filter> filters;
    /**
     * 排序
     */
    @ApiModelProperty(value = "排序")
    private List<Sort> sorts;

//    @ApiModelProperty(value = "convert measures to Map by FieldHash", dataType = "Map")
//    private Map<String, Measure> measureFieldHashMap;

    @Data
    @Builder
    public static class Dimension {
        @ApiModelProperty(name = "物理字段名")
        private String field;

        @ApiModelProperty(name = "字段hash值")
        private String fieldHash;

        @ApiModelProperty(name = "自定义表Hash值")
        private String tableHash;

        @ApiModelProperty(value = "计算表达式")
        private String expression;

        @ApiModelProperty(value = "是否计算", dataType = "Boolean", example = "true/false")
        private Flag calculatedFlag;
    }

    @Data
    @Builder
    public static class Measure {
        @ApiModelProperty(name = "物理字段名")
        private String field;

        @ApiModelProperty(name = "字段hash值")
        private String fieldHash;

        @ApiModelProperty(name = "数据类型", dataType = "String")
        private String dataType;

        @ApiModelProperty(name = "默认聚合", dataType = "String", example = "SUM COUNT AVG ... ...")
        private String defaultAggregator;

        @ApiModelProperty(name = "自定义表Hash值")
        private String tableHash;

        @ApiModelProperty(value = "计算表达式")
        private String expression;

        @ApiModelProperty(value = "是否计算", dataType = "Boolean", example = "true/false")
        private Flag calculatedFlag;
    }

    @Data
    @Builder
    public static class Condition {
        @ApiModelProperty(name = "表Hash值")
        private String tableHash;

        @ApiModelProperty(name = "物理表字段名")
        private String field;

        @ApiModelProperty(name = "字段hash值")
        private String fieldHash;

        @ApiModelProperty(name = "数据类型")
        private ColumnDataTypeEnum dataType;

        @ApiModelProperty(name = "运算符")
        private String operator;

        @ApiModelProperty(name = "字段值")
        private String fieldValue;

        @ApiModelProperty(value = "计算表达式")
        private String expression;

        @ApiModelProperty(value = "是否计算", dataType = "Boolean", example = "true/false")
        private Flag calculatedFlag;
    }

    /**
     * 自定义查询结果集筛选条件
     */
    @Data
    public static class Filter {
        @ApiModelProperty(value = "字段hash值")
        private String fieldHash;

        @ApiModelProperty(value = "运算符 between,in,=,>= ... ...")
        private String operator;

        @ApiModelProperty(value = "筛选值")
        private String value;

        @ApiModelProperty(value = "计算表达式")
        private String expression;

        @ApiModelProperty(value = "是否计算", dataType = "Boolean", example = "true/false")
        private Integer calculatedFlag;
    }

    /**
     * 排序
     */
    @Data
    @Builder
    public static class Sort {
        @ApiModelProperty(name = "表Hash值")
        private String tableHash;

        @ApiModelProperty(value = "排序字段")
        private String field;

        @ApiModelProperty(value = "排序字段Hash")
        private String fieldHash;

        @ApiModelProperty(name = "默认聚合", dataType = "String", example = "SUM COUNT AVG ... ...")
        private String defaultAggregator;

        @ApiModelProperty(value = "排序类型 asc desc")
        private String sortType;

        @ApiModelProperty(value = "计算表达式")
        private String expression;

        @ApiModelProperty(value = "是否计算", dataType = "Boolean", example = "true/false")
        private Integer calculatedFlag;
    }

    @Data
    @Builder
    public static class ResultColumn {
        @ApiModelProperty(name = "表Hash值")
        private String tableHash;

        @ApiModelProperty(name = "物理表字段名")
        private String field;

        @ApiModelProperty(name = "字段hash值")
        private String fieldHash;

        @ApiModelProperty(name = "列类型", dataType = "String", example = "dimension | measure")
        private ColumnTypeEnum columnType;

        @ApiModelProperty(name = "默认聚合", dataType = "String", example = "SUM COUNT AVG ... ...")
        private String defaultAggregator;
    }
}
